import Vue from 'vue';
import Vuex from 'vuex';
import api from '../api';
import constants from '../utils/constants';

Vue.use(Vuex);

const {
    ACCESS_TOKEN,
    USER_INFO
} = constants;

export default new Vuex.Store({
    state: {
        token: '',
        userId: '',
        username: '',
        realname: '',
        welcome: '',
        avatar: ''
    },
    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token
        },
        SET_NAME: (state, {
            username,
            realname,
            welcome
        }) => {
            state.username = username
            state.realname = realname
            state.welcome = welcome
        },
        SET_AVATAR: (state, avatar) => {
            state.avatar = avatar
        }
    },
    actions: {
        mLogin({commit}, userInfo) {
            return new Promise((resolve, reject) => {
                api.login(userInfo)
                    .then(response => {
                        if (response.data.code === 200) {
                            const userInfo = response.data.data;
                            window.localStorage.setItem(ACCESS_TOKEN, JSON.stringify(userInfo));
                            window.localStorage.setItem(USER_INFO, userInfo);
                            resolve(response)
                        } else {
                            reject(response)
                        }
                    })
                    .catch(error => {
                        console.log("catch===>response", error)
                        reject(error)
                    })
            })
        },
        // 账号密码登录
        passwordLogin({commit}, userInfo) {
            console.log('here: ', userInfo);
            return new Promise((resolve, reject) => {
                api.passwordLogin(userInfo).then(response => {
                    console.log('here: ', response);
                    if (response.data.code == 200) {
                        const userInfo = response.data.data;
                        window.localStorage.setItem(ACCESS_TOKEN, JSON.stringify(userInfo));
                        window.localStorage.setItem(USER_INFO, userInfo);
                        resolve(response)
                    } else {
                        reject(response)
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        },
        //手机号登录
        PhoneLogin({commit}, userInfo) {
            return new Promise((resolve, reject) => {
                api.phoneNoLogin(userInfo).then(response => {
                    if (response.data.code == 200) {
                        const result = response.data.result
                        const userInfo = result.userInfo
                        window.localStorage.setItem(ACCESS_TOKEN, result.token);
                        window.localStorage.setItem(USER_INFO, userInfo);
                        commit('SET_TOKEN', result.token)
                        commit('SET_NAME', {
                            username: userInfo.username,
                            realname: userInfo.realname
                        })
                        commit('SET_AVATAR', userInfo.avatar)
                        resolve(response)
                    } else {
                        reject(response)
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        },
        // 登出
        Logout({commit, state}) {
            return new Promise((resolve) => {
                let logoutToken = state.token;
                commit('SET_TOKEN', '')
                window.localStorage.removeItem(ACCESS_TOKEN)
                api.logout(logoutToken).then(() => {
                    resolve()
                }).catch(() => {
                    resolve()
                })
            })
        },

    },
    getters: {
        token: state => state.token,
        username: state => {
            state.userid = window.localStorage.getItem(USER_INFO).username;
            return state.username
        },
        nickname: state => {
            state.userid = window.localStorage.getItem(USER_INFO).realname;
            return state.user.realname
        },
        avatar: state => {
            state.userid = window.localStorage.getItem(USER_INFO).avatar;
            return state.user.avatar
        },
        userid: state => {
            state.userid = window.localStorage.getItem(USER_INFO).id;
            return state.userid
        },
    }
})
